#!/usr/bin/env python3

# Copyright (c) 2019, NVIDIA CORPORATION.  All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This script parses the report file generated by Catapult Design Checker
# to prepare a CSV summary.

# It takes no inputs; it searches the working directory recursively
# for files named design_checks_violated.log.

import re
from pathlib import Path
import pandas as pd

file_list =[]
for filename in Path('.').glob('**/design_checks_violated.log'):
    file_list.append(filename)
df = pd.DataFrame(columns=['Type','Rule','File','Message','Design'])
type_re = re.compile('^\s*\*\*\*\*\s+([A-Z]*)')
violation_re = re.compile('([A-Z]+)\s+(\w+.(?:h|cpp):[0-9]+,[0-9]+)\s+(.*)')
row = 0
for filename in file_list:
    print(filename)
    filelines = open(str(filename),'r').readlines()
    violation_type='' # Warning, Fatal, Error, Info
    for line in filelines:
        if (type_re.match(line)):
            m = type_re.match(line)
            violation_type = m.group(1)
        if (violation_re.match(line)):
            m = violation_re.match(line)
            rule = m.group(1)
            source = m.group(2)
            message = m.group(3).strip('\n')
            if (not re.search('spec_wrapper',source)):
                df.loc[row] = violation_type, rule, source, message, filename
                row += 1

df.to_csv('DesignCheckSummary.csv',index=False)
